using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

public class Clone_Skill_Controller : MonoBehaviour
{

    private float cloneTimer;// 克隆体存在计时器
    private float attackMultiplier;

    private SpriteRenderer sr;// 管理透明度渲染
    private Animator anim;// 动画

    [SerializeField] private float colorLoosingSpeed;// 变透明速度

    [Tooltip("检测攻击碰撞的实体")]
    public Transform attackCheck;
    [Tooltip("攻击范围半径")]
    public float attackCheckRadius = .8f;

    private Transform closestEnemy;// 最近的敌人
    private bool canDuplicateClone;//幻影再幻影开关
    private float chanceToDuplicate = 35;//几率

    private int facingDir = 1;//幻影的幻影朝向

    private void Awake()
    {
        sr = GetComponent<SpriteRenderer>();
        anim = GetComponent<Animator>();
    }

    private void Update()
    {
        cloneTimer -= Time.deltaTime;

        if (cloneTimer < 0)
        {
            //逐渐透明 sr.color.a透明度
            sr.color = new Color(1, 1, 1, sr.color.a - (Time.deltaTime * colorLoosingSpeed));
            if (sr.color.a <= 0)
            {
                Destroy(gameObject);//销毁克隆体
            }
        }
    }
    // 设置克隆体位置、持续时间、是否能攻击、最近的敌人
    public void SetupClone(Transform _newTramsfrom, float _cloneDuration, bool _canAttack, Vector3 _offset, Transform _closestEnemy, bool _canDuplicateClone, float _chanceToDuplicate, float _attackMultiplier)
    {
        if (_canAttack)//限制克隆体是否可以攻击，可以后期获取道具才解锁
        {
            anim.SetInteger("AttackNumber", Random.Range(1, 4));//随机攻击3中攻击方式，1~3
        }
        attackMultiplier = _attackMultiplier;
        transform.position = _newTramsfrom.position + _offset;//设置clone体位置
        cloneTimer = _cloneDuration;//设置定时器时间
        closestEnemy = _closestEnemy;//最近的敌人
        chanceToDuplicate = _chanceToDuplicate;//几率
        canDuplicateClone = _canDuplicateClone;//幻影再幻影
        FaceClosestTarget();
    }

    // 动画结束，开始变透明
    private void AnimationFinishTrigger()
    {
        cloneTimer = -.1f;
    }

    // 动画攻击事件
    private void AttackTrigger()
    {
        // 获取攻击圆圈内所有碰撞体
        Collider2D[] colliders = Physics2D.OverlapCircleAll(attackCheck.position, attackCheckRadius);

        foreach (var hit in colliders)
        {
            // 判断敌人
            if (hit.GetComponent<Enemy>() != null)
            {
                AudioManager.instance.PlaySFX(2, null);
                // hit.GetComponent<Enemy>().Damage();
                hit.GetComponent<Entity>().SetupKnockbackDir(transform);
                PlayerStats playerStats = PlayerManager.instance.player.GetComponent<PlayerStats>();
                playerStats.CloneDoDamage(hit.GetComponent<EnemyStats>(), attackMultiplier);

                if (PlayerManager.instance.player.skill.clone.canApplyOnHitEffect)
                {
                    // 对敌人造成武器效果，判断是否有武器
                    Inventory.instance.GetEquipment(EquipmentType.Weapon)?.ExecuteItemEffect(hit.transform);
                }

                if (canDuplicateClone)//幻影再幻影
                {
                    if (Random.Range(0, 100) < chanceToDuplicate)//几率产生幻影
                    {
                        SkillManager.instance.clone.CreateClone(hit.transform, new Vector3(.5f * facingDir, 0));
                    }
                }
            }
        }
    }

    //确定克隆体的朝向
    private void FaceClosestTarget()
    {
        if (closestEnemy != null)// 存在最近的敌人，转向
        {
            if (transform.position.x > closestEnemy.position.x)
            {
                facingDir = -1;
                transform.Rotate(0, 180, 0);
            }
        }
    }



}
